package com.author.surface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.text.StyledEditorKit;

public final class ItalicAction extends JToggleButton {
	/** Width and height of the ITALIC action button */
	private static final int	ITALIC_BUTTON_WIDTH		= 30;
	private static final int	ITALIC_BUTTON_HEIGHT	= 30;

	/** Font used to display the "I" letter inside the ITALIC action button */
	private static final Font	italicButtonFont		= new Font(
																"Courier New",
																Font.ITALIC, 24);

	/**
	 * X and Y co-ordinates which will be used to position the "I" letter inside
	 * the ITALIC action button
	 */
	private static final int	TX_						= 8;
	private static final int	TY_						= 23;

	/** characters that are used for display inside the ITALIC action button */
	public static final String	ITALIC					= "I";

	/** Italic action for the Styled Editor kit */
	private static class italicAction extends StyledEditorKit.ItalicAction {
		public italicAction() {
			super();
			putValue(NAME, ""); // name is not used
			putValue(ACCELERATOR_KEY,
					KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			super.actionPerformed(event);
		}
	};

	/**
	 * Italic action when invoked by the user will set the characters in the
	 * pane italicized. This will usually be invoked through the CTRL+I
	 * combination of keystrokes
	 */
	private static italicAction	italic_action	= new italicAction();

	public ItalicAction() {
		// Add italic action to the Toggle Button
		super(italic_action);

		// Set size of the ITALIC button
		setSize(new Dimension(ITALIC_BUTTON_WIDTH, ITALIC_BUTTON_HEIGHT + 1));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the ITALIC button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setToolTipText("Italic");
		setFocusable(false);
		setVisible(true);

		// Make the button invoke the action when a specific sequence of
		// keystrokes is fired by the user
		getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK),
				"ctrl i");
		getActionMap().put("ctrl i", italic_action);
	}

	/**
	 * This method will be executed when the UI updater requests that the button
	 * responsible for displaying Italic font is either selected or de-selected
	 */
	public void setItalic(boolean value) {
		if (value == true) {
			if (!isSelected()) {
				setSelected(true);
			}
		}

		if (value == false) {
			if (isSelected()) {
				setSelected(false);
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		FontRenderContext frc = g2d.getFontRenderContext();
		GlyphVector glyphs = italicButtonFont.createGlyphVector(frc, ITALIC);

		g2d.drawGlyphVector(glyphs, TX_, TY_);
	}
}
